package com.gitee.wsl.struct.dict

import com.gitee.wsl.struct.dict.DictionaryExt.getDictionary
import com.gitee.wsl.data.store.StoreServer

/**
 * 入口
 */
fun <T : Any,V:Any> sampleDictionary(state: StoreServer, dictName:String): DictionaryMap<T, V>{
    return getDictionary(state, dictName)
}

fun <T : Any,V:Any> sampleCacheDictionary(creator:(T)->V): DictionaryMap<T, V>{
    return CacheDictionaryMapImp(creator = creator)
}

fun <T : Any,V:Any> DictionaryMap<T, V>.flush(state: StoreServer){
    DictionaryExt.flushDictionary(state,this)
}


data class DictionaryMapImp<T:Any,V:Any>(override val name:String, val valueMap: MutableMap<T,V> = mutableMapOf()):DictionaryMap<T,V>{

    override fun put(key:T, value:V){
        valueMap[key]= value
    }

    override fun get(key:T):V?{
        return valueMap[key]
    }

    override fun delete(key:T){
        valueMap.remove(key)
    }

    override fun map(): Map<T, V> =valueMap
}

